<?php
/**
 * 来店管控
 * @author qianxun
 *
 */
class ReachStore extends MY_Controller{
    function ReachStore(){
        parent::MY_Controller();
        
    }
    /**
     * 专营店来店管控列表
     */
    function storeList(){
        //条件
        $where = array();
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $where['year'] = $year;
        //时段
        $this->load->model('timeUnitModel');
        $timeUnit = $this->input->get('timeUnit');
        //$timeUnit || $timeUnit = $this->timeUnitModel->getNowTimeUnit();
        $timeUnit && $where['timeUnit'] = $timeUnit;
        $this->viewData['timeUnit'] = $timeUnit;
        //时段列表
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        if ($timeUnit){
            $timeUnitList = isset($timeUnitDataList[$year][$timeUnit]) ? array($timeUnit=>$timeUnitDataList[$year][$timeUnit]) : array();
        }else{
            $timeUnitList = isset($timeUnitDataList[$year]) ? $timeUnitDataList[$year] : array();
        }
        $this->viewData['timeUnitList'] = $timeUnitList;
        //载入模型
        $this->load->model('reachTimeUnitModel');
        $this->load->model('reachWeekModel');
        //双月列表
        $unitDataList = $this->reachTimeUnitModel->getStoreListByTimeUnit($this->user['userId'], $year);
        $this->viewData['unitDataList'] = $unitDataList;
        //周列表
        $weekDataList = $this->reachWeekModel->getStoreListByWeek($this->user['userId'], $year);
        $this->viewData['weekDataList'] = $weekDataList;
        //来店量达成标准进度
        $this->load->model('configModel');
        $this->viewData['reachStandard'] = $this->configModel->getConfigData('reachStandard');
        
        $this->load->view('ReachStore/storeList', $this->viewData);
    }
    /**
     * 管理端来店管控列表
     */
    function adminList(){
        //条件
        $where = array();
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $where['year'] = $year;
        //时段
        $this->load->model('timeUnitModel');
        $timeUnit = $this->input->get('timeUnit');
        $timeUnit || $timeUnit = $this->timeUnitModel->getNowTimeUnit();
        $timeUnit && $where['timeUnit'] = $timeUnit;
        $this->viewData['timeUnit'] = $timeUnit;
        //周
        $weekNum = $this->input->get('weekNum');
        //$weekNum && $where['weekNum'] = $weekNum;
        $this->viewData['weekNum'] = $weekNum;
        //时段数据
        $timeUnitDataList = $this->timeUnitModel->getCacheData('timeUnitDataList');
        $timeUnitData = isset($timeUnitDataList[$year][$timeUnit]) ? $timeUnitDataList[$year][$timeUnit] : array();
        if ($weekNum){
            foreach ((array)$timeUnitData['weekList'] as $k => $v){
                if ($weekNum != $k){
                    unset($timeUnitData['weekList'][$k]);
                }
            }
        }
        $this->viewData['timeUnitData'] = $timeUnitData;
        //载入模型
        $this->load->model('reachTimeUnitModel');
        $this->load->model('reachWeekModel');
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, '', $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        //分页
        $count = $this->reachTimeUnitModel->getCount($where);
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl($_GET['c'], $_GET['m'], $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        
        $list = $this->reachTimeUnitModel->getNewList($where, $limit, $page, '', 'id desc');
        $list = $this->reachWeekModel->combineList($list);
        //echo json_encode($list);exit();
        $this->viewData['list'] = $list;
        //来店量达成标准进度
        $this->load->model('configModel');
        $this->viewData['reachStandard'] = $this->configModel->getConfigData('reachStandard');
        $this->load->view('ReachStore/adminList', $this->viewData);
    }
    /**
     * 红黑版
     */
    function topList(){
        $type = $this->input->get('type');
        in_array($type, array('red','black')) || $type = 'red';
        $this->navbar = array(
            array('title'=>'红版','url'=>printUrl('ReachStore','topList', array('type'=>'red')),'focusTag'=>'topList_red'),
            array('title'=>'黑版','url'=>printUrl('ReachStore','topList', array('type'=>'black')),'focusTag'=>'topList_black'),
        );
        $this->viewData['focusTag'] = $_GET['m'].'_'.$type;
        //条件
        $where = array();
        //年
        $year = $this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $where['year'] = $year;
        //时段
        $this->load->model('timeUnitModel');
        $timeUnit = $this->input->get('timeUnit');
        //$timeUnit || $timeUnit = $this->timeUnitModel->getNowTimeUnit();
        $timeUnit && $where['timeUnit'] = $timeUnit;
        $this->viewData['timeUnit'] = $timeUnit;
        //载入模型
        $this->load->model('reachTimeUnitModel');
        $count = $this->reachTimeUnitModel->getCount($where);
        $this->viewData['total'] = $count;
        $limit = ceil($count*0.1);
        $this->viewData['limit'] = $limit;
        if ($type == 'red'){
            $list = $this->reachTimeUnitModel->getNewList($where, $limit, 0, '', 'comeStoreRate desc');
        }else{
            $list = $this->reachTimeUnitModel->getNewList($where, $limit, $count-$limit, '', 'comeStoreRate desc');
        }
        $list = array_values($list);
        //组合专营店数据
        $this->load->model('sellpointModel');
        $this->viewData['list'] = $this->sellpointModel->combineStoreData($list);
        //var_dump($this->viewData['list']);
        //来店量达成标准进度
        $this->load->model('configModel');
        $this->viewData['reachStandard'] = $this->configModel->getConfigData('reachStandard');
        
        $this->load->view('ReachStore/topList', $this->viewData);
    }
}